/**
 * Tree对象
 * @since 1.0 <2015-8-3> SoChishun Added.
 */
var Tree = {
    newInstance: function () {
        var obj = {
            /**
             * 默认设置
             * @type JSON
             * @since 1.0 2015-8-3 SoChishun Added.
             */
            base_config: {
                tree_id: '#main_tree',
                tree_queryParams: '',
            },
            /**
             * 表格配置
             * @type JSON
             * @since 1.0 2015-8-3 SoChishun Added.
             */
            tree_config: {checkbox: true, cascadeCheck: false,
                onBeforeLoad: function () {
                },
                onLoadSuccess: function () {
                },
                onLoadError: function (data) {
                    alert(data.statusText);
                },
            },
            /**
             * 表格设置
             * <br />必需在set_base_config()之前执行
             * @param JSON options
             * @since 1.0 2015-8-3 SoChishun Added.
             */
            set_tree_config: function (options) {
                $.extend(this.tree_config, options);
            },
            /**
             * 基本设置
             * <br />必需在set_tree_config()之前执行
             * @param JSON options
             * @since 1.0 <2015-8-3> SoChishun Added.
             */
            set_base_config: function (options) {
                $.extend(this.base_config, options);
            },
            /**
             * 全选/取消全选 所有节点
             * @param boolean checked
             * @since 1.0 <2015-8-3> SoChishun Added.
             * @example <input type="checkbox" title="全选/取消全选" onclick="tree.tree_check(this.checked)" />全选
             */
            tree_check: function (checked) {
                var roots = $(this.base_config.tree_id).tree('getRoots');//返回tree的所有根节点数组              
                this._tree_check(roots, checked ? 'check' : 'uncheck');
            },
            /**
             * 递归函数
             * @param array noods
             * @param string checked_status 值为check或uncheck
             * @since 1.0 <2015-8-3> SoChishun Added.
             */
            _tree_check: function (noods, checked_status) {
                for (var i = 0; i < noods.length; i++) {
                    var node = $(this.base_config.tree_id).tree('find', noods[i].id);//查找节点  
                    $(this.base_config.tree_id).tree((checked_status), node.target);//将得到的节点选中  
                    var nodes = $(this.base_config.tree_id).tree('getChildren', node.target);
                    this._tree_check(nodes, checked_status);
                }
            },
            /**
             * 获取选中的值
             * <br />值之间以英文逗号隔开
             * @returns string
             * @since 1.0 <2015-8-3> SoChishun
             */
            get_checked: function (field) {
                var nodes = $(this.base_config.tree_id).tree('getChecked'); // , ['checked', 'indeterminate']
                if (!field) {
                    field = 'id';
                }
                var s = '';
                for (var i = 0; i < nodes.length; i++) {
                    if (s != '') {
                        s += ',';
                    }
                    s += nodes[i][field];
                }
                return s;
            },
            /**
             * 初始化
             * @param JSON options
             * @since 1.0 2015-8-3 SoChishun Added.
             */
            init: function (options) {
                if (undefined != options) {
                    $.extend(this.base_config, options);
                }
                // 初始化tree
                if (this.base_config.tree_id && this.tree_config) {
                    var $tree = $(this.base_config.tree_id);
                    if (!this.tree_config.url) {
                        this.tree_config.url = $tree.data('url');
                    }
                    // 读取grid中的data-queryParams
                    if ($tree.data('queryParams')) {
                        this.tree_config.queryParams = $.parseJSON('{' + $tree.data('queryParams') + '}');
                    }
                    // 读取隐藏配置 datagrid_queryParams_id
                    if (this.base_config.tree_queryParams) {
                        this.tree_config.queryParams = $.parseJSON('{' + $(this.base_config.tree_queryParams).html() + '}');
                    }
                    $tree.tree(this.tree_config);
                }
            },
        };
        return obj;
    }
}